package com.adcc.sgs.gcs.proxy;


import com.adcc.sgs.gcs.db.DBAccess;
import com.adcc.sgs.gcs.entity.QueueEntity;
import com.adcc.sgs.gcs.service.SendQueueService;
import com.google.common.base.Optional;
import com.smartUtility.common.log.Log;
import com.smartUtility.mq.client.mq.MQClient;
import com.smartUtility.mq.client.mq.MQMessage;
import com.smartUtility.mq.client.mq.MQMsgListener;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.setup.Environment;


/**
 * Created by liuyi on 2016/8/16.
 */
public class AdapterProxy implements Managed, MQMsgListener {
    // Active MQ Recvive 客户端
    private Optional<MQClient> activemqRecvClient = Optional.absent();

    //Active MQ Send 客户端
    private Optional<MQClient> activemqSendClient = Optional.absent();

    private Optional<DBAccess> dbAccess;

    private static AdapterProxy instance;

    private AdapterProxy() {
    }

    /**
     * 单例
     */
    public static AdapterProxy getInstance() {
        if (instance == null) {
            instance = new AdapterProxy();
        }
        return instance;
    }

    public void build(Environment environment) {
        environment.lifecycle().manage(this);
    }

    @Override
    public void start() throws Exception {
        activemqRecvClient.get().setMsgListener(this);
    }

    @Override
    public void stop() throws Exception {
        try {
            if (this.activemqSendClient.isPresent()) {
                this.activemqSendClient.get().stop();
            }
            if (this.activemqRecvClient.isPresent()) {
                this.activemqRecvClient.get().stop();
            }
        } catch (Exception ex) {
            Log.error(getClass().getName(), "stop error", ex);
        }
    }

    @Override
    public void onMQMessage(Optional<MQMessage> recMessage) {
        try {
            if (recMessage.isPresent()) {
                if (recMessage.get().getContent().isPresent()) {
                    String recStr = recMessage.get().getContent().get();
                    Log.info(getClass().getName(), "receive message from:" + recMessage.get().getName().get() + "\r\n" + recStr);

                    // 转发
                    SendQueueService sendQueueService = new SendQueueService();
                    QueueEntity queueEntity = sendQueueService.findSendQueue(3);
                    sendQueueService.sendMessageToDestination(queueEntity, recStr);
                }
            }
        } catch (Exception ex) {
            Log.error(getClass().getName(), "onMQMessage() error", ex);
        }
    }

    public Optional<MQClient> getActivemqRecvClient() {
        return activemqRecvClient;
    }

    public void setActivemqRecvClient(Optional<MQClient> activemqRecvClient) {
        this.activemqRecvClient = activemqRecvClient;
    }

    public Optional<MQClient> getActivemqSendClient() {
        return activemqSendClient;
    }

    public void setActivemqSendClient(Optional<MQClient> activemqSendClient) {
        this.activemqSendClient = activemqSendClient;
    }

    public Optional<DBAccess> getDbAccess() {
        return dbAccess;
    }

    public void setDbAccess(Optional<DBAccess> dbAccess) {
        this.dbAccess = dbAccess;
    }
}
